@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row">
            <!-- 日历 -->
            <div class="col-12 mb-4">
                <div class="card shadow-sm">
                    <div class="card-header bg-white">
                        <div class="d-flex justify-content-between align-items-center">
                            <h5 class="mb-0 text-primary">
                                <i class="fas fa-calendar-alt me-2"></i>作业日历
                            </h5>
                            <a href="{{ route('assignments.create') }}" class="btn btn-primary btn-sm">
                                <i class="fas fa-plus"></i> 创建作业
                            </a>
                        </div>
                    </div>
                    <div class="card-body">
                        <form id="dateFilterForm" method="GET" action="{{ route('assignments.index') }}">
                            <div class="row">
                                <div class="col-12 mb-3">
                                    <input type="date" class="form-control" id="due_date" name="date"
                                        placeholder="选择日期查看作业" required value="{{ request('date') }}"
                                        onclick="this.showPicker()" onchange="this.form.submit()">
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

            <!-- 作业列表 -->
            <div class="col-12">
                <div class="card shadow-sm">
                    <div class="card-header bg-white">
                        <div class="d-flex justify-content-between align-items-center">
                            <h5 class="mb-0 text-primary">
                                <i class="fas fa-tasks me-2"></i>作业列表
                            </h5>
                            @if (request()->has('date'))
                                <a href="{{ route('assignments.index') }}" class="btn btn-outline-primary btn-sm">
                                    <i class="fas fa-times"></i> 回到今天
                                </a>
                            @endif
                        </div>
                    </div>

                    <div class="card-body">
                        @if ($assignments->isEmpty())
                            <div class="text-center py-5">
                                <i class="fas fa-clipboard-list fa-3x text-muted mb-3"></i>
                                <p class="text-muted">
                                    @if (request()->has('date'))
                                        该日期暂无作业
                                    @else
                                        暂无作业
                                    @endif
                                </p>
                            </div>
                        @else
                            @foreach ($assignments as $assignment)
                                <div class="assignment-card mb-3">
                                    <div class="d-flex flex-column">
                                        <div class="mb-3 {{ $assignment->completions->isNotEmpty() && $assignment->completions->first()->is_completed ? 'text-muted' : '' }}">
                                            <div class="d-flex align-items-center mb-2">
                                                <h5 class="card-title {{ $assignment->completions->isNotEmpty() && $assignment->completions->first()->is_completed ? 'text-muted' : 'text-primary' }} mb-0">{{ $assignment->title }}</h5>
                                            </div>
                                            <p class="card-text mb-3" style="white-space: pre-line;">
                                                {{ $assignment->content }}</p>
                                            <div class="d-flex flex-wrap gap-2 mb-2">
                                                @if ($assignment->type === 'daily')
                                                    <span class="badge {{ $assignment->completions->isNotEmpty() && $assignment->completions->first()->is_completed ? 'bg-secondary' : 'bg-success' }}"><i class="fas fa-calendar"></i>
                                                        每日作业</span>
                                                @elseif($assignment->type === 'weekly')
                                                    <span class="badge {{ $assignment->completions->isNotEmpty() && $assignment->completions->first()->is_completed ? 'bg-secondary' : 'bg-warning' }}"><i class="fas fa-calendar"></i>
                                                        每周作业</span>
                                                @else
                                                    <span class="badge {{ $assignment->completions->isNotEmpty() && $assignment->completions->first()->is_completed ? 'bg-secondary' : 'bg-info' }}"><i class="fas fa-calendar"></i> 每日打卡</span>
                                                @endif
                                            </div>
                                        </div>
                                        <div class="d-flex gap-2">
                                            <form action="{{ route('assignments.complete', $assignment) }}" method="POST" class="d-inline">
                                                @csrf
                                                <button type="submit"
                                                    class="btn btn-sm {{ $assignment->completions->isNotEmpty() && $assignment->completions->first()->is_completed ? 'btn-success disabled' : 'btn-outline-success' }}"
                                                    {{ $assignment->completions->isNotEmpty() && $assignment->completions->first()->is_completed ? 'disabled' : '' }}>
                                                    <i class="fas fa-check"></i>
                                                    {{ $assignment->completions->isNotEmpty() && $assignment->completions->first()->is_completed ? '已完成' : '标记完成' }}
                                                </button>
                                            </form>
                                            <a href="{{ route('assignments.edit', $assignment) }}"
                                                class="btn btn-sm btn-primary">
                                                <i class="fas fa-edit"></i> 编辑
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            @endforeach
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('styles')
    <style>
        .assignment-card {
            background: white;
            border-radius: 10px;
            padding: 1.25rem;
            transition: all 0.3s ease;
            border: 1px solid rgba(0, 0, 0, 0.1);
        }

        .assignment-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        .badge {
            padding: 0.5em 0.8em;
            font-weight: 500;
        }

        /* 日历样式 */
        .calendar-container {
            background: white;
            border-radius: 10px;
            padding: 1rem;
        }

        .flatpickr-calendar {
            width: 100% !important;
            box-shadow: none !important;
            border: 1px solid rgba(0, 0, 0, 0.1) !important;
        }

        .flatpickr-day.selected {
            background: var(--primary-color) !important;
            border-color: var(--primary-color) !important;
        }

        .flatpickr-day.today {
            border-color: var(--primary-color) !important;
        }

        .flatpickr-day.has-assignment {
            background: rgba(74, 144, 226, 0.1) !important;
        }

        .flatpickr-day.has-assignment::after {
            content: '';
            position: absolute;
            bottom: 2px;
            left: 50%;
            transform: translateX(-50%);
            width: 4px;
            height: 4px;
            background: var(--primary-color);
            border-radius: 50%;
        }

        @media (max-width: 768px) {
            .flatpickr-calendar {
                font-size: 14px;
            }

            .flatpickr-day {
                height: 35px;
                line-height: 35px;
            }

            .assignment-card {
                padding: 1rem;
            }

            .card-title {
                font-size: 1.1rem;
            }

            .badge {
                font-size: 0.8em;
                padding: 0.4em 0.6em;
            }

            .btn {
                padding: 0.25rem 0.5rem;
                font-size: 0.9em;
            }
        }
    </style>
@endpush

@push('scripts')
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 设置日期选择器的默认值为当前日期
            const dateInput = document.getElementById('due_date');
            if (!dateInput.value) {
                const today = new Date();
                const formattedDate = today.toISOString().split('T')[0];
                dateInput.value = formattedDate;
            }
        });
    </script>
@endpush
